package com.bgs.eat.roleBack.service.impl;

import com.bgs.eat.page.MyPageHelper;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.roleBack.entity.RoleBackEntity;
import com.bgs.eat.roleBack.mapper.RoleBackMapper;
import com.bgs.eat.roleBack.service.RoleBackService;
import com.bgs.eat.userBack.entity.UserBackEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @ClassName RoleBackServiceImpl
 * @Description TODO
 * @Author cjl
 * @Date 2022/10/12 8:40
 **/
@Service
public class RoleBackServiceImpl implements RoleBackService {

    @Autowired(required = false)
    RoleBackMapper roleBackMapper;

    /**
    * @Author cjl
    * @Description 全查角色（模糊查角色名称）
    * @Date  9:08
    * @Return com.bgs.eat.page.PageResult
    * @param roleName
    * @param pageRequest
    */
    @Override
    public PageResult selectRoleBackEntity(String roleName, PageRequest pageRequest) {
        if (StringUtils.isEmpty(roleName)){
            roleName="";
        }
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(RoleBackEntity.class);
        example.createCriteria().andLike("roleName","%"+roleName+"%")
                .andEqualTo("delFlag",0);
        List<RoleBackEntity> roleBackEntityList = roleBackMapper.selectByExample(example);
        return MyPageHelper.pageInfo(roleBackEntityList);
    }

    /**
    * @Author cjl
    * @Description 添加或修改角色信息
    * @Date  15:46
    * @Return int
    * @param roleBackEntity
    */
    @Override
    public int saveOrUpdateRoleBackEntity(RoleBackEntity roleBackEntity) {
        int i = 0;
        if (roleBackEntity.getId() == 0 ){
            roleBackEntity.setCreateTime(new Date());
            roleBackEntity.setUpdateTime(new Date());
            roleBackEntity.setDelFlag(0);
            i = roleBackMapper.insertSelective(roleBackEntity);
        }else {
            roleBackEntity.setUpdateTime(new Date());
            i = roleBackMapper.updateByPrimaryKeySelective(roleBackEntity);
        }
        return i;
    }

    /**
    * @Author cjl
    * @Description 删除角色信息
    * @Date  15:47
    * @Return java.lang.Integer
    * @param id
    */
    @Override
    public Integer deleteRoleBackEntityById(Integer id) {
        Example example = new Example(RoleBackEntity.class);
        example.createCriteria().andEqualTo("id",id);
        RoleBackEntity roleBackEntity = new RoleBackEntity();
        roleBackEntity.setDelFlag(1);
        int i = roleBackMapper.updateByExampleSelective(roleBackEntity, example);
        return i;
    }

    @Override
    public RoleBackEntity getRoleById(Integer id) {
        RoleBackEntity roleBackEntity = roleBackMapper.selectByPrimaryKey(id);
        return roleBackEntity;
    }

    @Override
    public int assignPermissions(RoleBackEntity roleBackEntity) {
        int i = roleBackMapper.updateByPrimaryKeySelective(roleBackEntity);
        return i;
    }

    @Override
    public List<RoleBackEntity> roleBackEntityByRoleId() {
        Example example = new Example(RoleBackEntity.class);
        example.createCriteria().andEqualTo("delFlag",0);
        List<RoleBackEntity> list = roleBackMapper.selectByExample(example);
        return list;
    }
}
